import Router from '@easyroute/core';
import hashMode from '@easyroute/core/hash-mode';
// import { token, loginUser } from '../utils/stores';

import Layout from '../views/Layout.svelte';
import AdminLayout from '../views/AdminLayout.svelte';
import Login from '../views/Login.svelte';
import AdminIndex from '../components/admin/Index.svelte';
import AddArt from '../components/article/AddArt.svelte';
import ArtList from '../components/article/ArtList.svelte';
import CateList from '../components/category/CateList.svelte';
import NotFound from '../views/404.svelte';


let currentToken = null;
let currentUser = null;


// 路由定义
export const router = new Router({
    mode: hashMode,
    omitTrailingSlash: true,
    routes: [
        {
            path: '/',
            component: Layout,
            name: 'layout',
            meta: { title: '' },
            children: [
                {
                    path: 'admin',
                    component: AdminLayout,
                    name: 'index',
                    meta: { title: '' },
                    // beforeEnter: auth,
                    beforeEnter: (to, from, next) => {
                        let token = window.sessionStorage.getItem("token");
                        if (token) {
                            next();
                        } else {
                            next('/login');
                        }
                    },
                    children: [
                        {
                            path: '/index',
                            component: AdminIndex,
                            name: 'index',
                            meta: { title: '管理首页' },
                        },
                        {
                            path: '/addart',
                            component: AddArt,
                            name: 'addart',
                            meta: { title: '添加文章' },
                        },
                        {
                            path: '/addart/:id',
                            component: AddArt,
                            name: 'addart',
                            meta: { title: '编辑文章' },

                        },
                        {
                            path: '/artList',
                            component: ArtList,
                            name: 'artList',
                            meta: { title: '文章列表' },
                        },
                        {
                            path: '/cateList',
                            component: CateList,
                            name: 'cateList',
                            meta: { title: '分类列表' },
                        }
                    ]
                },
                {
                    path: 'login',
                    component: Login,
                    name: 'login',
                    meta: { title: '登录' },
                }
            ]
        },
        {
            path: '*',
            name: '404',
            meta: { title: '404' },
            component: NotFound,
        }

    ]
});

export default router;